package office.stock.model;

import java.util.Date;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.ManyToOne;
import javax.persistence.NamedQuery;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;

import lombok.Getter;
import lombok.Setter;

@Setter
@Getter
@Entity
@NamedQuery(name="Offer.getAllActive", query = "SELECT offer FROM Offer offer WHERE offer.sold = false OR offer.sellingDate > :date ORDER BY offer.creationDate DESC")
public class Offer extends BaseEntity {

    private static final long serialVersionUID = 1L;

    private String title;
    
    private Double price;
    
    private SupportedCurrency currency;
    
    @Column(length = 4096)
    private String description;
    
    @Temporal(TemporalType.TIMESTAMP)
    private Date creationDate;
    
    private boolean sold;
    
    @Temporal(TemporalType.TIMESTAMP)
    private Date sellingDate;
    
    @ManyToOne(cascade={CascadeType.MERGE})
    private User seller;
    
    @ManyToOne(cascade={CascadeType.MERGE})
    private User newOwner;
    
    private String imageId;
    
    public Offer(String title, Double price, String description, User seller) {
	super();
	this.title = title;
	this.price = price;
	this.description = description;
	this.seller = seller;
	this.creationDate = new Date();
    }

    public Offer() {
    }

    public void setSold(boolean sold) {
	this.sold = sold;
	if (sold) {
	    sellingDate = new Date();
	}
    }
    
}
